﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace RL
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            UpdateRoutine();
        }

        private void UpdateRoutine()
        {
            if (Properties.Settings.Default.checkSchedule)
            {
                try
                {
                    routelistEntities db = new routelistEntities();
                    Schedule item = (from o in db.Schedule where o.monday == true && o.tuesday == true && o.wednesday == true && o.thursday == true && o.friday == true && o.saturday == false && o.sunday == false select o).FirstOrDefault();
                    if (null == item)
                    {
                        item = new Schedule { monday = true, tuesday = true, wednesday = true, thursday = true, friday = true, saturday = false, sunday = false };
                        db.Schedule.AddObject(item);
                        db.SaveChanges();
                        Properties.Settings.Default.checkSchedule = false;
                        Properties.Settings.Default.Save();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            };
        }

        private void EditPersons(object sender, RoutedEventArgs e)
        {
            wPerson wnd = new wPerson();
            wnd.Show();
        }

        private void EditRouteType(object sender, RoutedEventArgs e)
        {
            wRouteType wnd = new wRouteType();
            wnd.Show();
        }

        private void ShowRoutePL_Click(object sender, RoutedEventArgs e)
        {
            Form1 frm = new Form1();
            frm.Show();

        }

        private void CreateDB_Click(object sender, RoutedEventArgs e)
        {
            WindowCreateDatabase wnd = new WindowCreateDatabase();
            wnd.Show();
        }

        private void EditRoute_Click(object sender, RoutedEventArgs e)
        {
            wRouteRegistry wnd = new wRouteRegistry();
            wnd.Show();
        }

        private void Departure_Click(object sender, RoutedEventArgs e)
        {
            wRouteDeparture wnd = new wRouteDeparture();
            wnd.Show();
        }

        private void FuelType_Click(object sender, RoutedEventArgs e)
        {
            wFuelType wnd = new wFuelType();
            wnd.Show();
        }

        private void VehicleType_Click(object sender, RoutedEventArgs e)
        {
            wVehicleType wnd = new wVehicleType();
            wnd.Show();
        }

        private void Vehicle_Click(object sender, RoutedEventArgs e)
        {
            wVehicle wnd = new wVehicle();
            wnd.ContextDB = new routelistEntities();
            wnd.Show();
        }

        private void RouteListJournal_Click(object sender, RoutedEventArgs e)
        {
            wRouteList wnd = new wRouteList();
            wnd.Show();
        }

        private void Enterprise_Click(object sender, RoutedEventArgs e)
        {
            wEnterpriseInfo wnd = new wEnterpriseInfo();
            wnd.Show();
        }

        private void VokzalTableJournal_Click(object sender, RoutedEventArgs e)
        {
            wStationMoney wnd = new wStationMoney();
            wnd.Show();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            wUpdate wnd = new wUpdate();
            wnd.Update();
            try
            {
                wnd.Close();
            }
            catch (Exception)
            {
            }
        }

        private void TechReport_Click(object sender, RoutedEventArgs e)
        {
            FormTechReport frm = new FormTechReport();
            frm.Show();
        }

        private void Cordb_Click(object sender, RoutedEventArgs e)
        {
            routelistEntities routelistEntities = new RL.routelistEntities();
            var rl = from o in routelistEntities.RouteList select o;
            foreach (RouteList r in rl)
            {
                if (r.dt_datedoc.HasValue)
                    r.dt_datedoc = r.dt_datedoc.Value.Date;
            }
            routelistEntities.SaveChanges();
        }

        private void Station_Click(object sender, RoutedEventArgs e)
        {
            wStation frm = new wStation();
            frm.Show();
        }

        private void RouteListCash_Click(object sender, RoutedEventArgs e)
        {
            wRLCash frm = new wRLCash();
            frm.Show();
        }

        private void TechReportByDriver_Click(object sender, RoutedEventArgs e)
        {
            FormTechReportByDriver frm = new FormTechReportByDriver();
            frm.Show();
        }

        private void TechReportByVehicleModel_Click(object sender, RoutedEventArgs e)
        {
            FormTechReportByVehicleModel frm = new FormTechReportByVehicleModel();
            frm.Show();
        }

        private void TechReportDetailByVehicle_Click(object sender, RoutedEventArgs e)
        {
            FormTechReportDetailByVehicleModel frm = new FormTechReportDetailByVehicleModel();
            frm.Show();
        }

        private void CalcInsurance_clcik(object sender, RoutedEventArgs e)
        {
            wRecalcInsurance wnd = new wRecalcInsurance();
            wnd.ShowDialog();
        }

        private void VehicleLength_Click(object sender, RoutedEventArgs e)
        {
            FormVehicleLength frm = new FormVehicleLength();
            frm.Show();
        }
    }
}
